package com.internetCafes.spms.web.customer.controller;

import com.internetCafes.spms.common.base.BaseWebController;
import com.internetCafes.spms.common.module.excel.ExcelUtils;
import com.internetCafes.spms.common.utils.entity.RR;
import com.internetCafes.spms.common.utils.entity.page.PageItemDTO;
import com.internetCafes.spms.common.utils.entity.page.PageItemVO;
import com.internetCafes.spms.core.annotation.SysLog;
import com.internetCafes.spms.web.common.model.DataMarkBaseReq;
import com.internetCafes.spms.web.customer.bizservice.customer.IEntCustomerBizService;
import com.internetCafes.spms.web.customer.model.ExcelImportResultRes;
import com.internetCafes.spms.web.customer.model.FileUploadRes;
import com.internetCafes.spms.web.customer.model.customer.*;
import com.internetCafes.spms.web.customer.model.customer.quali.QualiMatchReq;
import com.internetCafes.spms.web.customer.model.customer.quali.QualiMatchRes;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 客户信息表 前端控制器
 * </p>
 *
 * @author Zj
 * @since 2021-10-20
 */
@RestController
@RequestMapping("/ent/customer")
public class EntCustomerController extends BaseWebController {

    @Autowired
    private IEntCustomerBizService customerBizService;

    /**
     * 客户标记
     */
    @PostMapping("/mark")
    @RequiresPermissions("ent:customer:mark")
    public RR<Void> mark(@RequestBody DataMarkBaseReq req) {
        return customerBizService.mark(req) ? RR.success() : RR.fail();
    }

    /**
     * 客户信息验证
     */
    @PostMapping("/info/confirm")
    public RR<Void> infoConfirm(@RequestBody CustomerInfoConfirmReq req) {
        return customerBizService.infoConfirm(req) ? RR.success() : RR.fail();
    }

    /**
     * 批量保存
     */
    @PostMapping("/batch/save")
    @RequiresPermissions("ent:customer:save")
    public RR<ExcelImportResultRes> batchSave(@RequestBody CustomerBatchSaveReq req) {
        return RR.success(customerBizService.batchSave(req));
    }

    /**
     * 文件上传接口
     */
    @PostMapping("/file/upload")
    public RR<FileUploadRes> fileUpload(MultipartFile file) {
        return RR.success(customerBizService.fileUpload(file));
    }

    /**
     * 导入企业客户
     */
    @PostMapping("/ent/import")
    @RequiresPermissions("ent:customer:entImport")
    public RR<List<CustomerEntImportExcel>> importEntExcel(MultipartFile file) {
        return RR.success(customerBizService.entExcelImport(file));
    }

    /**
     * 导入个人客户
     */
    @PostMapping("/person/import")
    @RequiresPermissions("ent:customer:personImport")
    public RR<List<CustomerPersonImportExcel>> importPersonExcel(MultipartFile file) {
        return RR.success(customerBizService.personExcelImport(file));
    }

    /**
     * 导出
     */
    @PostMapping("/export")
    @RequiresPermissions("ent:customer:export")
    public void export(@RequestBody CustomerExcelReq req, HttpServletResponse response) {
        Workbook workbook = customerBizService.createCustomerExcel(req);
        ExcelUtils.downLoadExcel("customer.xls", response, workbook);
    }

    /**
     * 获取资质列表
     */
    @PostMapping("/quali/list")
    public RR<List<QualiMatchRes>> quliMatchList(@RequestBody QualiMatchReq req) {
        return RR.success(customerBizService.qualiMatchList(req));
    }

    /**
     * 获取可转让部门人员树
     */
    @PostMapping("/transfer/user/tree")
    public RR<List<CustomerTransferUserTreeRes>> transferUserTree(@RequestBody CustomerTransferUserTreeReq req) {
        return RR.success(customerBizService.customerTransferUserTree(req));
    }

    /**
     * 获取未跟进天数
     */
    @GetMapping("/count/{customerId}")
    public RR<CustomerCountRes> count(@PathVariable Long customerId) {
        return RR.success(customerBizService.customerCount(customerId));
    }

    /**
     * 资质匹配
     */
    @PostMapping("/quali/match")
    public RR<List<QualiMatchRes>> qualiMatch(@RequestBody QualiMatchReq req) {
        return RR.success(customerBizService.qualiMatch(req));
    }

    /**
     * 客户转让
     */
    @SysLog("客户转让")
    @PutMapping("/transfer")
    public RR<Void> customerTransfer(@RequestBody CustomerTransferReq req) {
        return customerBizService.customerTransfer(req) ? RR.success() : RR.fail();
    }

    /**
     * 标记为近期重要客户
     */
    @SysLog("标记为近期重要客户")
    @PutMapping("/mainly/cancel/{idList}")
    public RR<Void> mainlyCancel(@PathVariable List<Long> idList) {
        return customerBizService.mainlyCancel(idList) ? RR.success() : RR.fail();
    }

    /**
     * 标记为近期重要客户
     */
    @SysLog("标记为近期重要客户")
    @PutMapping("/mainly/{idList}")
    public RR<Void> mainly(@PathVariable List<Long> idList) {
        return customerBizService.mainly(idList) ? RR.success() : RR.fail();
    }

    /**
     * 客户信息详情
     */
    @GetMapping("/detail/{id}")
    @RequiresPermissions("ent:customer:detail")
    public RR<CustomerDetailRes> detail(@PathVariable Long id) {
        return RR.success(customerBizService.customerDetail(id));
    }

    /**
     * 客户信息分页
     */
    @PostMapping("/paging")
    @RequiresPermissions("ent:customer:paging")
    public RR<PageItemVO<CustomerPagingRes>> paging(@RequestBody PageItemDTO<CustomerPagingReq> req) {
        return RR.success(customerBizService.customerPaging(req));
    }

    /**
     * 客户信息编辑
     */
    @SysLog("客户信息编辑")
    @PutMapping()
    @RequiresPermissions("ent:customer:update")
    public RR<Void> update(@RequestBody CustomerUpdateReq req) {
        return customerBizService.update(req) ? RR.success() : RR.fail();
    }

    /**
     * 客户删除
     */
    @SysLog("客户删除")
    @DeleteMapping("/{idList}")
    @RequiresPermissions("ent:customer:delete")
    public RR<Void> delete(@PathVariable List<Long> idList) {
        return customerBizService.delete(idList) ? RR.success() : RR.fail("此账号下有关联子账号，无法删除");
    }

    /**
     * 客户新增
     */
    @SysLog("客户新增")
    @PostMapping("/save")
    @RequiresPermissions("ent:customer:save")
    public RR<Void> saveCustomer(@RequestBody CustomerSaveReq req) {
        return customerBizService.customerSave(req) ? RR.success() : RR.fail();
    }
}
